#UTF-8
install.packages('Cairo')

#每次加载以下库
require(PerformanceAnalytics)
require(quantmod)
require(fPortfolio)
require("jsonlite")
require("xts")
require("plotrix")
require("ggplot2")
require("readxl")
library(Cairo)
par(family = "Hei")

#收集上证指数收盘价数据
getSymbols('000001.SS', src = "yahoo")
SS000001_NoNa <- na.omit(`000001.SS`)
SS000001_close <- `000001.SS`$`000001.SS.Adjusted`
SS000001_ROC <- ROC(type='discrete', SS000001_close)
SS000001_ROC <- na.omit(SS000001_ROC)
colnames(SS000001_ROC) <- "SS000001.ROC"

#收集人民币美元收盘数据
getSymbols('CNYUSD=X', src = "yahoo")
CNYUSD_NoNa <- na.omit(`CNYUSD=X`)
CNYUSD_close <- CNYUSD_NoNa$`CNYUSD=X.Adjusted`
CNYUSD_ROC <- ROC(type='discrete', CNYUSD_close)
CNYUSD_ROC <- na.omit(CNYUSD_ROC)
colnames(CNYUSD_ROC) <- "CNYUSD.ROC"

#收集格里夫纳兑美元收盘数据
getSymbols('USDUAH=X', src = "yahoo")
USDUAH_NoNa <- na.omit(`USDUAH=X`)
UAHUSD_close <- 1 / USDUAH_NoNa$`USDUAH=X.Adjusted`
colnames(UAHUSD_close) <- "UAHUSD=X.Adjusted"
UAHUSD_ROC <- ROC(type='discrete', UAHUSD_close)
UAHUSD_ROC <- na.omit(UAHUSD_ROC)
colnames(UAHUSD_ROC) <- "UAHUSD.ROC"

#收集意大利FTSEMIB股票指数收盘数据
FTSEMIB.MI_NoNa <- read.csv('FtseMibHistoricalData.csv', head = TRUE, sep = ",") 
FTSEMIB.MI_zoo <- zoo(FTSEMIB.MI_NoNa[, -1], order.by = as.Date(FTSEMIB.MI_NoNa$Date))
FTSEMIB.MI_close <- FTSEMIB.MI_zoo[, 'FTSEMIB.MI.close']
FTSEMIB.MI_ROC <- FTSEMIB.MI_zoo[, 'FTSEMIB.MI.ROC']

#收集比特币价格信息
BtcMarketPrice_2days <- fromJSON("https://api.blockchain.info/charts/market-price?timespan=all&format=json")
BtcMarketPrice_1day <- fromJSON("https://api.blockchain.info/charts/market-price?timespan=3years&format=json")
BtcMarketPrice_2days_matrix <- cbind(BtcMarketPrice_2days$values$x, BtcMarketPrice_2days$values$y)
BtcMarketPrice_1day_matrix <- cbind(BtcMarketPrice_1day$values$x, BtcMarketPrice_1day$values$y)
BtcMarketPrice <- rbind(BtcMarketPrice_2days_matrix, BtcMarketPrice_1day_matrix)
BtcMarketPrice_order <- BtcMarketPrice[order(BtcMarketPrice[,1]), ]
BtcMarketPrice_NoZero <- BtcMarketPrice_order[which(BtcMarketPrice_order[, 2]!=0),]
BtcMarketPrice_unique <- unique(BtcMarketPrice_NoZero)
BtcMarketPrice_NoNA <- na.omit(BtcMarketPrice_unique)

#给列命名
colnames(BtcMarketPrice_NoNA) = c("time_stamp", "Btc_market_Price")

#把时间戳转换为日期格式
Btc_market_Price_time_stamp <- BtcMarketPrice_NoNA[, 1]
Btc_market_Price_date <- as.Date(as.POSIXlt(Btc_market_Price_time_stamp, origin = "1970/01/01", tz = "UTC"))
Btc_market_Price_and_date <- cbind(BtcMarketPrice_NoNA, Btc_market_Price_date)

#填充2天或3天间隔中间的数据
n_BtcMarketPrice_2days <- nrow(BtcMarketPrice_2days_matrix)
n_BtcMarketPrice_1day <- nrow(BtcMarketPrice_1day_matrix)
n_max <- n_BtcMarketPrice_2days * 2 + n_BtcMarketPrice_1day

Btc_market_Price_and_date <- subset(Btc_market_Price_and_date, select =  - time_stamp)
index_Btc_market_Price_and_date <- 1
index_Btc_market_Price_and_date_ingegrity <- 1
Btc_market_Price_and_date_ingegrity <- data.frame(Btc_market_Price = rep(NA, n_max), date = rep(NA, n_max))
for (index_Btc_market_Price_and_date in 1:nrow(Btc_market_Price_and_date)){
  if (index_Btc_market_Price_and_date < 2){
    Btc_market_Price_and_date_ingegrity$date[index_Btc_market_Price_and_date_ingegrity] <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 2]
    Btc_market_Price_and_date_ingegrity$Btc_market_Price[index_Btc_market_Price_and_date_ingegrity] <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 1]
  }else {
    days <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 2] - Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 2]
    if (days == 2) {
      Btc_market_Price_and_date_ingegrity$date[index_Btc_market_Price_and_date_ingegrity] <- (Btc_market_Price_and_date[index_Btc_market_Price_and_date, 2] + Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 2]) / 2
      Btc_market_Price_and_date_ingegrity$Btc_market_Price[index_Btc_market_Price_and_date_ingegrity] <- (Btc_market_Price_and_date[index_Btc_market_Price_and_date, 1] + Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 1]) / 2
      index_Btc_market_Price_and_date_ingegrity <- index_Btc_market_Price_and_date_ingegrity + 1
      Btc_market_Price_and_date_ingegrity$date[index_Btc_market_Price_and_date_ingegrity] <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 2]
      Btc_market_Price_and_date_ingegrity$Btc_market_Price[index_Btc_market_Price_and_date_ingegrity] <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 1]
    } else if(days == 1){
      Btc_market_Price_and_date_ingegrity$date[index_Btc_market_Price_and_date_ingegrity] <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 2]
      Btc_market_Price_and_date_ingegrity$Btc_market_Price[index_Btc_market_Price_and_date_ingegrity] <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 1]
    } else if(days == 3){
      Btc_market_Price_and_date_ingegrity$date[index_Btc_market_Price_and_date_ingegrity] <- (Btc_market_Price_and_date[index_Btc_market_Price_and_date, 2] - Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 2]) / 3 + Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 2]
      Btc_market_Price_and_date_ingegrity$Btc_market_Price[index_Btc_market_Price_and_date_ingegrity] <- (Btc_market_Price_and_date[index_Btc_market_Price_and_date, 1] - Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 1]) / 3 + Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 1]
      index_Btc_market_Price_and_date_ingegrity <- index_Btc_market_Price_and_date_ingegrity + 1
      Btc_market_Price_and_date_ingegrity$date[index_Btc_market_Price_and_date_ingegrity] <- (Btc_market_Price_and_date[index_Btc_market_Price_and_date, 2] - Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 2]) / 3 * 2 + Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 2]
      Btc_market_Price_and_date_ingegrity$Btc_market_Price[index_Btc_market_Price_and_date_ingegrity] <- (Btc_market_Price_and_date[index_Btc_market_Price_and_date, 1] - Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 1]) / 3 * 2 + Btc_market_Price_and_date[index_Btc_market_Price_and_date - 1, 1]
      index_Btc_market_Price_and_date_ingegrity <- index_Btc_market_Price_and_date_ingegrity + 1
      Btc_market_Price_and_date_ingegrity$date[index_Btc_market_Price_and_date_ingegrity] <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 2]
      Btc_market_Price_and_date_ingegrity$Btc_market_Price[index_Btc_market_Price_and_date_ingegrity] <- Btc_market_Price_and_date[index_Btc_market_Price_and_date, 1]
    }
  }
  index_Btc_market_Price_and_date_ingegrity <- index_Btc_market_Price_and_date_ingegrity + 1
}
Btc_market_Price_and_date_ingegrity_NoNa <- na.omit(Btc_market_Price_and_date_ingegrity)
Btc_market_Price_and_date_zoo <- zoo(Btc_market_Price_and_date_ingegrity_NoNa[1], order.by = as.Date(Btc_market_Price_and_date_ingegrity_NoNa[, 2], origin = "1970/01/01", tz = "UTC"))
roc_Btc_market_Price <- ROC(type='discrete', Btc_market_Price_and_date_zoo) 
roc_Btc_market_Price_NoNa <- na.omit(roc_Btc_market_Price)

#用共同的时间为关键字合并创建数据集
SS000001_ROC_zoo <- as.zoo(SS000001_ROC)
SS000001_and_Btc_market_Price_ROC <- merge.zoo(SS000001_ROC_zoo, "Btc_ROC" = roc_Btc_market_Price_NoNa, all = c("FALSE", "TRUE"), fill = 0)

CNYUSD_ROC_zoo <- as.zoo(CNYUSD_ROC)
CNYUSD_and_Btc_market_Price_ROC <- merge.zoo(CNYUSD_ROC_zoo, "Btc_ROC" = roc_Btc_market_Price_NoNa, all = c("FALSE", "TRUE"), fill = 0)

UAHUSD_ROC_zoo <- as.zoo(UAHUSD_ROC)
UAHUSD_and_Btc_market_Price_ROC <- merge.zoo(UAHUSD_ROC_zoo, "Btc_ROC" = roc_Btc_market_Price_NoNa, all = c("FALSE", "TRUE"), fill = 0)

FTSEMIB.MI_and_Btc_market_Price_ROC <- merge.zoo(FTSEMIB.MI_ROC, "Btc_ROC" = roc_Btc_market_Price_NoNa, all = c("FALSE", "TRUE"), fill = 0)

#计算年化收益率和标准差
SS000001_annualized_return <- table.AnnualizedReturns(SS000001_and_Btc_market_Price_ROC$SS000001.ROC, Rf = 0.0564/365)
Btc_annualized_return <- table.AnnualizedReturns(SS000001_and_Btc_market_Price_ROC$Btc_ROC, Rf = 0.0564/365)

CNYUSD_annualized_return <- table.AnnualizedReturns(CNYUSD_and_Btc_market_Price_ROC$CNYUSD.ROC, Rf = 0.0564/365)

UAHUSD_annualized_return <- table.AnnualizedReturns(UAHUSD_and_Btc_market_Price_ROC$UAHUSD.ROC, Rf = 0.0564/365)

FTSEMIB.MI_annualized_return <- table.AnnualizedReturns(FTSEMIB.MI_and_Btc_market_Price_ROC$FTSEMIB.MI_ROC, Rf = 0.0564/365)

#计算相关系数
SS000001_close_zoo <- as.zoo(SS000001_close)
SS000001_close_and_Btc_ROC <- merge.zoo(SS000001_close_zoo, SS000001_and_Btc_market_Price_ROC, all = c("FALSE", "TRUE"), fill = NA)
SS000001_and_Btc_close_and_ROC <- merge.zoo(Btc_market_Price_and_date_zoo, SS000001_close_and_Btc_ROC,  all = c("FALSE", "TRUE"), fill = NA)
SS000001_and_Btc_close_and_ROC_full <- na.approx(SS000001_and_Btc_close_and_ROC)
SS000001_and_Btc_close_and_ROC_full <- na.omit(SS000001_and_Btc_close_and_ROC_full)
cor_SS000001_and_Btc <- cor(SS000001_and_Btc_close_and_ROC_full)

CNYUSD_close_zoo <- as.zoo(CNYUSD_close)
CNYUSD_close_and_Btc_ROC <- merge.zoo(CNYUSD_close_zoo, CNYUSD_and_Btc_market_Price_ROC, all = c("FALSE", "TRUE"), fill = NA)
CNYUSD_and_Btc_close_and_ROC <- merge.zoo(Btc_market_Price_and_date_zoo, CNYUSD_close_and_Btc_ROC,  all = c("FALSE", "TRUE"), fill = NA)
CNYUSD_and_Btc_close_and_ROC_full <- na.approx(CNYUSD_and_Btc_close_and_ROC)
CNYUSD_and_Btc_close_and_ROC_full <- na.omit(CNYUSD_and_Btc_close_and_ROC_full)
cor_CNYUSD_and_Btc <- cor(CNYUSD_and_Btc_close_and_ROC_full)

UAHUSD_close_zoo <- as.zoo(UAHUSD_close)
UAHUSD_close_and_Btc_ROC <- merge.zoo(UAHUSD_close_zoo, UAHUSD_and_Btc_market_Price_ROC, all = c("FALSE", "TRUE"), fill = NA)
UAHUSD_and_Btc_close_and_ROC <- merge.zoo(Btc_market_Price_and_date_zoo, UAHUSD_close_and_Btc_ROC,  all = c("FALSE", "TRUE"), fill = NA)
UAHUSD_and_Btc_close_and_ROC_full <- na.approx(UAHUSD_and_Btc_close_and_ROC)
UAHUSD_and_Btc_close_and_ROC_full <- na.omit(UAHUSD_and_Btc_close_and_ROC_full)
cor_UAHUSD_and_Btc <- cor(UAHUSD_and_Btc_close_and_ROC_full)

FTSEMIB.MI_close_and_Btc_ROC <- merge.zoo(FTSEMIB.MI_close, FTSEMIB.MI_and_Btc_market_Price_ROC, all = c("FALSE", "TRUE"), fill = NA)
FTSEMIB.MI_and_Btc_close_and_ROC <- merge.zoo(Btc_market_Price_and_date_zoo, FTSEMIB.MI_close_and_Btc_ROC,  all = c("FALSE", "TRUE"), fill = NA)
FTSEMIB.MI_and_Btc_close_and_ROC_full <- na.approx(FTSEMIB.MI_and_Btc_close_and_ROC)
FTSEMIB.MI_and_Btc_close_and_ROC_full <- na.omit(FTSEMIB.MI_and_Btc_close_and_ROC_full)
cor_FTSEMIB.MI_and_Btc <- cor(FTSEMIB.MI_and_Btc_close_and_ROC_full)

#建立有效前沿投资组合
SS000001_and_Btc_ROC_ts <- as.timeSeries(SS000001_and_Btc_market_Price_ROC)
efficient_portfolio = efficientPortfolio(SS000001_and_Btc_ROC_ts, spec = portfolioSpec(), constraints = "LongOnly")
efficient_portfolio
efficient_portfolio_ROC_ts <- SS000001_and_Btc_market_Price_ROC[, 1] * getWeights(efficient_portfolio)[1] + SS000001_and_Btc_market_Price_ROC[, 2] * getWeights(efficient_portfolio)[2]

CNYUSD_and_Btc_ROC_ts <- as.timeSeries(CNYUSD_and_Btc_market_Price_ROC)
efficient_portfolio4 = efficientPortfolio(CNYUSD_and_Btc_ROC_ts, spec = portfolioSpec(), constraints = "LongOnly")
efficient_portfolio4
efficient_portfolio4_ROC_ts <- CNYUSD_and_Btc_market_Price_ROC[, 1] * getWeights(efficient_portfolio4)[1] + CNYUSD_and_Btc_market_Price_ROC[, 2] * getWeights(efficient_portfolio4)[2]

UAHUSD_and_Btc_ROC_ts <- as.timeSeries(UAHUSD_and_Btc_market_Price_ROC)
efficient_portfolio5 = efficientPortfolio(UAHUSD_and_Btc_ROC_ts, spec = portfolioSpec(), constraints = "LongOnly")
efficient_portfolio5
efficient_portfolio5_ROC_ts <- UAHUSD_and_Btc_market_Price_ROC[, 1] * getWeights(efficient_portfolio5)[1] + UAHUSD_and_Btc_market_Price_ROC[, 2] * getWeights(efficient_portfolio5)[2]

FTSEMIB.MI_and_Btc_ROC_ts <- as.timeSeries(FTSEMIB.MI_and_Btc_market_Price_ROC)
FTSEMIB.MI_and_Btc_efficient_portfolio = efficientPortfolio(FTSEMIB.MI_and_Btc_ROC_ts, spec = portfolioSpec(), constraints = "LongOnly")
FTSEMIB.MI_and_Btc_efficient_portfolio
FTSEMIB.MI_and_Btc_efficient_portfolio_ROC_ts <- FTSEMIB.MI_and_Btc_market_Price_ROC[, 1] * getWeights(FTSEMIB.MI_and_Btc_efficient_portfolio)[1] + FTSEMIB.MI_and_Btc_market_Price_ROC[, 2] * getWeights(FTSEMIB.MI_and_Btc_efficient_portfolio)[2]

#计算有效前沿投资组合的年化收益率和标准差
efficient_portfolio_annualized_return <- table.AnnualizedReturns(efficient_portfolio_ROC_ts, Rf = 0.0564/365)

efficient_portfolio4_annualized_return <- table.AnnualizedReturns(efficient_portfolio4_ROC_ts, Rf = 0.0564/365)

efficient_portfolio5_annualized_return <- table.AnnualizedReturns(efficient_portfolio5_ROC_ts, Rf = 0.0564/365)

FTSEMIB.MI_and_Btc_efficient_portfolio_annualized_return <- table.AnnualizedReturns(FTSEMIB.MI_and_Btc_efficient_portfolio_ROC_ts, Rf = 0.0564/365)

#1画上证指数和比特币走势图
twoord.plot(lx = index(SS000001_and_Btc_close_and_ROC_full), ly = SS000001_and_Btc_close_and_ROC_full$`000001.SS.Adjusted`, rx = index(SS000001_and_Btc_close_and_ROC_full), ry = SS000001_and_Btc_close_and_ROC_full$Btc_market_Price, main = "", xlab = "", ylab = "", rylab = "", type = c("line", "line"), lcol = "#CC6633", lwd = 2, xtickpos = as.numeric(index(SS000001_and_Btc_close_and_ROC_full)), xticklab = as.character(index(SS000001_and_Btc_close_and_ROC_full)), col.main = "#CC6633", fg = "#CC6633")
mtext("上证指数", col = "#CC6633", side = 2, line = 3, family = "Song")
mtext(paste("上证指数和比特币的相关系数为", round(cor_SS000001_and_Btc[1, 2], 2)), col = "#CC6633", cex = 1.5, side = 3, line = 2, family = "Song")
mtext("比特币价格 美元", col = "red", side = 4, line = 1, family = "Song")


#3画有效前沿投资组合权重图
efficient_portfolio_weight <- getWeights(efficient_portfolio)
efficient_portfolio_weight_group <- data.frame(group = c("上证指数", "比特币"), weight = c(efficient_portfolio_weight[1],efficient_portfolio_weight[2]))
rownames(efficient_portfolio_weight_group) <- NULL
image_ggplot <- ggplot(efficient_portfolio_weight_group, aes(x = "有效前沿投资组合成分", y=weight, fill=group)) + geom_bar(stat="identity")  + ylab("权重") 
ggsave("EfficientPortfolioWeightPlot.pdf", plot = image_ggplot, device = cairo_pdf, family = "Song", width=10, height=8)

#4画上证指数、比特币和有效前沿投资组合的年化收益率和标准差图
rownames(SS000001_annualized_return) <- NULL
rownames(Btc_annualized_return) <- NULL
rownames(efficient_portfolio_annualized_return) <- NULL
SS000001_annualized_return_row <-t(SS000001_annualized_return)
Btc_annualized_return_row <-t(Btc_annualized_return)
efficient_portfolio_annualized_return_row <-t(efficient_portfolio_annualized_return)
annualized_return_all <- rbind(SS000001_annualized_return_row, Btc_annualized_return_row, efficient_portfolio_annualized_return_row)
colnames(annualized_return_all) <- c("annualized_return", "annualized_standard_deviation", "annualized_sharpe")
annualized_return_all_DataFrame <- as.data.frame(annualized_return_all)
annualized_return_all_DataFrame$annualized_sharpe[1] <- - annualized_return_all_DataFrame$annualized_sharpe[1]
annualized_return_all_DataFrame$group <- c("上证指数", "比特币", "有效前沿投资组合")
symbols(annualized_return_all_DataFrame$annualized_return * 100, annualized_return_all_DataFrame$annualized_standard_deviation * 100, circle = annualized_return_all_DataFrame$annualized_sharpe * 100,
        inches=0.3, xlab = "年化收益率 %", ylab = "年化波动率 %", main = "比特币上证指数有效前沿组合有更高的收益相同的风险", cex.main = 1.3,
        bg= "#CC6633", col.main = "#CC6633", fg = "black", col.lab = "#CC6633", col.axis = "#CC6633", family = "Hei")
mtext("上证指数", side = 1, at = -8, cex = 1, col = "#CC6633", family = "Hei")
mtext("有效前沿投资组合", side = 1, at = 25, cex = 1, col = "#CC6633", family = "Hei")
mtext("比特币", side = 1, at = 150, cex = 1, col = "#CC6633", family = "Hei")
mtext( "圆大小-年化夏普比率", side = 1, line = 4, col = "#CC6633", family = "Hei")

#5画上证指数、有效前沿投资组合的累计收益率图
efficient_portfolio_ROC_zoo <- as.zoo(efficient_portfolio_ROC_ts)
efficient_portfolio_and_SS000001_ROC <- merge.zoo('efficient_portfolio.ROC' = efficient_portfolio_ROC_zoo, SS000001_ROC_zoo, all = c("TRUE", "FALSE"), fill = 0)
efficient_portfolio_and_SS000001_ROC_xts <- as.xts(efficient_portfolio_and_SS000001_ROC)
charts.PerformanceSummary(efficient_portfolio_and_SS000001_ROC_xts, main = '上证指数和比特币上证指数有效前沿投资组合的累计收益率', geometric = TRUE, legend.loc = 'topright')

#6画人民币兑美元和比特币走势图
twoord.plot(lx = index(CNYUSD_and_Btc_close_and_ROC_full), ly = CNYUSD_and_Btc_close_and_ROC_full$`CNYUSD=X.Adjusted`, rx = index(CNYUSD_and_Btc_close_and_ROC_full), ry = CNYUSD_and_Btc_close_and_ROC_full$Btc_market_Price, main = "", xlab = "", ylab = "", rylab = "", type = c("line", "line"), lcol = "#CC6633", lwd = 2, xtickpos = as.numeric(index(CNYUSD_and_Btc_close_and_ROC_full)), xticklab = as.character(index(CNYUSD_and_Btc_close_and_ROC_full)), col.main = "#CC6633", fg = "#CC6633")
mtext("人民币兑美元 美元", col = "#CC6633", side = 2, line = 3, family = "Song")
mtext(paste("人民币兑美元和比特币的相关系数为", round(cor_CNYUSD_and_Btc[1, 2], 2)), col = "#CC6633", cex = 1.5, side = 3, line = 2, family = "Song")
mtext("比特币价格 美元", col = "red", side = 4, line = 1, family = "Song")

#8画人民币兑美元和比特币有效前沿投资组合权重图
efficient_portfolio4_weight <- getWeights(efficient_portfolio4)
efficient_portfolio4_weight_group <- data.frame(group = c("人民币兑美元", "比特币"), weight = c(efficient_portfolio4_weight[1],efficient_portfolio4_weight[2]))
rownames(efficient_portfolio4_weight_group) <- NULL
image_ggplot4 <- ggplot(efficient_portfolio4_weight_group, aes(x = "有效前沿投资组合成分", y=weight, fill=group)) + geom_bar(stat="identity")  + ylab("权重") 
ggsave("EfficientPortfolio4WeightPlot.pdf", plot = image_ggplot4, device = cairo_pdf, family = "Song", width=10, height=8)

#9画人民币兑美元、比特币和有效前沿投资组合的年化收益率和标准差图
rownames(CNYUSD_annualized_return) <- NULL
rownames(Btc_annualized_return) <- NULL
rownames(efficient_portfolio4_annualized_return) <- NULL
CNYUSD_annualized_return_row <-t(CNYUSD_annualized_return)
Btc_annualized_return_row <-t(Btc_annualized_return)
efficient_portfolio4_annualized_return_row <-t(efficient_portfolio4_annualized_return)
annualized_return4_all <- rbind(CNYUSD_annualized_return_row, efficient_portfolio4_annualized_return_row, Btc_annualized_return_row)
colnames(annualized_return4_all) <- c("annualized_return", "annualized_standard_deviation", "annualized_sharpe")
annualized_return4_all_DataFrame <- as.data.frame(annualized_return4_all)
annualized_return4_all_DataFrame$group <- c("人民币兑美元", "有效前沿投资组合", "比特币")
annualized_return4_all_DataFrame$annualized_sharpe[1] <- - annualized_return4_all_DataFrame$annualized_sharpe[1]
annualized_return4_all_DataFrame$annualized_sharpe[2] <- - annualized_return4_all_DataFrame$annualized_sharpe[2]
symbols(annualized_return4_all_DataFrame$annualized_return * 100, annualized_return4_all_DataFrame$annualized_standard_deviation * 100, circle = annualized_return4_all_DataFrame$annualized_sharpe * 100,
        inches = 0.5, xlab = "年化收益率 %", ylab = "年化波动率 %", main = '',
        bg= c("#CC6633", 'red', "#CC6633"), col.main = "#CC6633", fg = "black", col.lab = "#CC6633", col.axis = "#CC6633", family = "Song")
mtext("人民币兑美元", side = 1, at = -15, cex = 0, col = "#CC6633", family = "Song")
mtext("有效前沿投资组合", side = 1, at = 21, cex = 1, col = "red", family = "Song")
mtext("比特币", side = 1, at = 150, cex = 1, col = "#CC6633", family = "Song")
mtext( "圆大小-年化夏普比率", side = 1, line = 4, col = "#CC6633", family = "Song")
mtext("人民币兑美元比特币有效前沿组合有更高的收益相同的风险", cex = 1.3, side =3,  line = 1, col = "#CC6633", family = "Song")

#10画人民币兑美元、有效前沿投资组合的累计收益率图
efficient_portfolio4_ROC_zoo <- as.zoo(efficient_portfolio4_ROC_ts)
efficient_portfolio4_and_CNYUSD_ROC <- merge.zoo('efficient_portfolio.ROC' = efficient_portfolio4_ROC_zoo, CNYUSD_ROC_zoo, all = c("TRUE", "FALSE"), fill = 0)
efficient_portfolio4_and_CNYUSD_ROC_xts <- as.xts(efficient_portfolio4_and_CNYUSD_ROC)
charts.PerformanceSummary(efficient_portfolio4_and_CNYUSD_ROC_xts, main = '人民币兑美元和比特币人民币兑美元有效前沿投资组合的累计收益率', geometric = TRUE, legend.loc = 'topright')

#11画格里夫纳兑美元和比特币走势图
par(family = "Hei")
twoord.plot(lx = index(UAHUSD_and_Btc_close_and_ROC_full), ly = UAHUSD_and_Btc_close_and_ROC_full$`UAHUSD=X.Adjusted`, rx = index(UAHUSD_and_Btc_close_and_ROC_full), ry = UAHUSD_and_Btc_close_and_ROC_full$Btc_market_Price, main = "", xlab = "", ylab = "", rylab = "", type = c("line", "line"), lcol = "#CC6633", lwd = 2, xtickpos = as.numeric(index(UAHUSD_and_Btc_close_and_ROC_full)), xticklab = as.character(index(UAHUSD_and_Btc_close_and_ROC_full)), col.main = "#CC6633", fg = "#CC6633")
mtext("格里夫纳兑美元 美元", col = "#CC6633", side = 2, line = 3)
mtext(paste("格里夫纳兑美元和比特币的相关系数为", round(cor_UAHUSD_and_Btc[1, 2], 2)), col = "#CC6633", cex = 1.5, side = 3, line = 2, family = "Song")
mtext("比特币价格 美元", col = "red", side = 4, line = 1, family = "Hei")

#13画格里夫纳兑美元和比特币有效前沿投资组合权重图
efficient_portfolio5_weight <- getWeights(efficient_portfolio5)
efficient_portfolio5_weight_group <- data.frame(group = c("格里夫纳兑美元", "比特币"), weight = c(efficient_portfolio5_weight[1],efficient_portfolio5_weight[2]))
rownames(efficient_portfolio5_weight_group) <- NULL
image_ggplot5 <- ggplot(efficient_portfolio5_weight_group, aes(x = "有效前沿投资组合成分", y=weight, fill=group)) + geom_bar(stat="identity")  + ylab("权重") 
ggsave("UahUsdAndBtcEfficientPortfolioWeightPlot.pdf", plot = image_ggplot5, device = cairo_pdf, family = "Song", width=10, height=8)

#14画格里夫纳兑美元、比特币和有效前沿投资组合的年化收益率和标准差图
rownames(UAHUSD_annualized_return) <- NULL
rownames(Btc_annualized_return) <- NULL
rownames(efficient_portfolio5_annualized_return) <- NULL
UAHUSD_annualized_return_row <-t(UAHUSD_annualized_return)
Btc_annualized_return_row <-t(Btc_annualized_return)
efficient_portfolio5_annualized_return_row <-t(efficient_portfolio5_annualized_return)
annualized_return5_all <- rbind(UAHUSD_annualized_return_row, efficient_portfolio5_annualized_return_row, Btc_annualized_return_row)
colnames(annualized_return5_all) <- c("annualized_return", "annualized_standard_deviation", "annualized_sharpe")
annualized_return5_all_DataFrame <- as.data.frame(annualized_return5_all)
annualized_return5_all_DataFrame$group <- c("格里夫纳兑美元", "有效前沿投资组合", "比特币")
annualized_return5_all_DataFrame$annualized_sharpe[1] <- - annualized_return5_all_DataFrame$annualized_sharpe[1]
annualized_return5_all_DataFrame$annualized_sharpe[2] <- - annualized_return5_all_DataFrame$annualized_sharpe[2]
symbols(annualized_return5_all_DataFrame$annualized_return * 100, annualized_return5_all_DataFrame$annualized_standard_deviation * 100, circle = annualized_return5_all_DataFrame$annualized_sharpe * 100,
        inches = 0.5, xlab = "年化收益率 %", ylab = "年化波动率 %", main = '',
        bg= c("#CC6633", 'black', "#CC6633"), col.main = "#CC6633", fg = "black", col.lab = "#CC6633", col.axis = "#CC6633", family = "Song")
mtext("格里夫纳兑美元", side = 1, at = -23, cex = 0, col = "#CC6633", family = "Song")
mtext("有效前沿投资组合", side = 1, at = 20, cex = 1, col = "black", family = "Song")
mtext("比特币", side = 1, at = 150, cex = 1, col = "#CC6633", family = "Song")
mtext( "圆大小-年化夏普比率", side = 1, line = 4, col = "#CC6633", family = "Song")
mtext("格里夫纳兑美元比特币有效前沿组合有更高的收益相同的风险", cex = 1.3, side =3,  line = 1, col = "#CC6633", family = "Song")

#15画格里夫纳兑美元、有效前沿投资组合的累计收益率图
efficient_portfolio5_ROC_zoo <- as.zoo(efficient_portfolio5_ROC_ts)
efficient_portfolio5_and_UAHUSD_ROC <- merge.zoo('efficient_portfolio.ROC' = efficient_portfolio5_ROC_zoo, UAHUSD_ROC_zoo, all = c("TRUE", "FALSE"), fill = 0)
efficient_portfolio5_and_UAHUSD_ROC_xts <- as.xts(efficient_portfolio5_and_UAHUSD_ROC)
charts.PerformanceSummary(efficient_portfolio5_and_UAHUSD_ROC_xts, main = '格里夫纳和比特币格里夫纳有效前沿投资组合的累计收益率', geometric = TRUE, legend.loc = 'topright')

#16画意大利富时MIB股票指数和比特币走势图
twoord.plot(lx = index(FTSEMIB.MI_and_Btc_close_and_ROC_full), ly = FTSEMIB.MI_and_Btc_close_and_ROC_full$FTSEMIB.MI_close, rx = index(FTSEMIB.MI_and_Btc_close_and_ROC_full), ry = FTSEMIB.MI_and_Btc_close_and_ROC_full$Btc_market_Price, main = "", xlab = "", ylab = "", rylab = "", type = c("line", "line"), lcol = "#CC6633", lwd = 2, xtickpos = as.numeric(index(FTSEMIB.MI_and_Btc_close_and_ROC_full)), xticklab = as.character(index(FTSEMIB.MI_and_Btc_close_and_ROC_full)), col.main = "#CC6633", fg = "#CC6633")
mtext("意大利富时MIB股票指数 欧元", col = "#CC6633", side = 2, line = 3)
mtext(paste("意大利富时MIB股票指数和比特币的相关系数为", round(cor_FTSEMIB.MI_and_Btc[1, 2], 2)), col = "#CC6633", cex = 1.5, side = 3, line = 2, family = "Song")
mtext("比特币价格 美元", col = "red", side = 4, line = 1, family = "Hei")

#18画意大利富时MIB股票指数和比特币有效前沿投资组合权重图
FTSEMIB.MI_and_Btc_efficient_portfolio_weight <- getWeights(FTSEMIB.MI_and_Btc_efficient_portfolio)
FTSEMIB.MI_and_Btc_efficient_portfolio_weight_group <- data.frame(group = c("意大利富时MIB股票指数", "比特币"), weight = c(FTSEMIB.MI_and_Btc_efficient_portfolio_weight[1],FTSEMIB.MI_and_Btc_efficient_portfolio_weight[2]))
rownames(FTSEMIB.MI_and_Btc_efficient_portfolio_weight_group) <- NULL
FTSEMIB.MI_and_Btc_image_ggplot <- ggplot(FTSEMIB.MI_and_Btc_efficient_portfolio_weight_group, aes(x = "有效前沿投资组合成分", y=weight, fill=group)) + geom_bar(stat="identity")  + ylab("权重") 
ggsave("FTSEMIB.MIAndBtcEfficientPortfolioWeightPlot.pdf", plot = FTSEMIB.MI_and_Btc_image_ggplot, device = cairo_pdf, family = "Song", width=10, height=8)

#19画意大利富时MIB股票指数、比特币和有效前沿投资组合的年化收益率和标准差图
rownames(FTSEMIB.MI_annualized_return) <- NULL
rownames(Btc_annualized_return) <- NULL
rownames(FTSEMIB.MI_and_Btc_efficient_portfolio_annualized_return) <- NULL
FTSEMIB.MI_annualized_return_row <-t(FTSEMIB.MI_annualized_return)
Btc_annualized_return_row <-t(Btc_annualized_return)
FTSEMIB.MI_and_Btc_efficient_portfolio_annualized_return_row <-t(FTSEMIB.MI_and_Btc_efficient_portfolio_annualized_return)
FTSEMIB.MI_and_Btc_annualized_return_all <- rbind(FTSEMIB.MI_annualized_return_row, FTSEMIB.MI_and_Btc_efficient_portfolio_annualized_return_row, Btc_annualized_return_row)
colnames(FTSEMIB.MI_and_Btc_annualized_return_all) <- c("annualized_return", "annualized_standard_deviation", "annualized_sharpe")
FTSEMIB.MI_and_Btc_annualized_return_all_DataFrame <- as.data.frame(FTSEMIB.MI_and_Btc_annualized_return_all)
FTSEMIB.MI_and_Btc_annualized_return_all_DataFrame$group <- c("意大利富时MIB股票指数", "有效前沿投资组合", "比特币")
FTSEMIB.MI_and_Btc_annualized_return_all_DataFrame$annualized_sharpe[1] <- - FTSEMIB.MI_and_Btc_annualized_return_all_DataFrame$annualized_sharpe[1]
symbols(FTSEMIB.MI_and_Btc_annualized_return_all_DataFrame$annualized_return * 100, FTSEMIB.MI_and_Btc_annualized_return_all_DataFrame$annualized_standard_deviation * 100, circle = FTSEMIB.MI_and_Btc_annualized_return_all_DataFrame$annualized_sharpe * 100,
        inches = 0.5, xlab = "年化收益率 %", ylab = "年化波动率 %", main = '',
        bg= c("#CC6633", 'black', "#CC6633"), col.main = "#CC6633", fg = "black", col.lab = "#CC6633", col.axis = "#CC6633", family = "Song")
mtext("意大利富时MIB股票指数", side = 1, at = -18, cex = 0, col = "#CC6633", family = "Song")
mtext("有效前沿投资组合", side = 1, at = 35, cex = 1, col = "black", family = "Song")
mtext("比特币", side = 1, at = 150, cex = 1, col = "#CC6633", family = "Song")
mtext( "圆大小-年化夏普比率", side = 1, line = 4, col = "#CC6633", family = "Song")
mtext("意大利富时MIB股票指数比特币有效前沿组合有更高的收益相同的风险", cex = 1.2, side =3,  line = 1, col = "#CC6633", family = "Song")

#20画意大利富时MIB股票指数、有效前沿投资组合的累计收益率图
FTSEMIB.MI_and_Btc_efficient_portfolio_ROC_zoo <- as.zoo(FTSEMIB.MI_and_Btc_efficient_portfolio_ROC_ts)
FTSEMIB.MI_and_Btc_efficient_portfolio_and_FTSEMIB.MI_ROC <- merge.zoo('efficient_portfolio.ROC' = FTSEMIB.MI_and_Btc_efficient_portfolio_ROC_zoo, FTSEMIB.MI_ROC, all = c("TRUE", "FALSE"), fill = 0)
FTSEMIB.MI_and_Btc_efficient_portfolio_and_FTSEMIB.MI_ROC_xts <- as.xts(FTSEMIB.MI_and_Btc_efficient_portfolio_and_FTSEMIB.MI_ROC)
charts.PerformanceSummary(FTSEMIB.MI_and_Btc_efficient_portfolio_and_FTSEMIB.MI_ROC_xts, main = '比特币意大利富时MIB股票指数有效前沿组合的累计收益率', geometric = TRUE, legend.loc = 'topleft')
